import http from '@/utils/http'
import type { ICategory } from '@/types'

// 新增
export const categoryAdd = (payload: ICategory) => {
  return http.post('/api/manage/category', payload);
}

// 修改
export const categoryEdit = (payload: ICategory) => {
  return http.put(`/api/manage/category`, payload);
}

// 新增或修改
export const categoryAddOrEdit = (payload: ICategory) => {
  // 识别新增或修改，根据id是否有值判断
  if (payload.id) {
    return categoryEdit(payload); // 修改
  } else {
    return categoryAdd(payload); // 新增
  }
}

// 删除
export const categoryDel = (id: number) => {
  return http.delete(`/api/manage/category/${id}`);
}

// 批量删除
export const categoryBatchDel = (delIds: number[]) => {
  const delTasks: Promise<unknown>[] = delIds.map(id => http.delete(`/api/manage/category/${id}`));
  return Promise.allSettled(delTasks);
}

// 查询所有（前端搜索分页）
export const categoryList = () => {
  return http.get('/api/manage/category/tree');
}

// 查询指定id
export const categoryById = (id: number) => {
  return http.get(`/api/manage/category/${id}`);
}

